package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.dao.WebUnionidDao;
import ysq.xyg.web.model.po.WebUnionid;
import ysq.xyg.web.model.vo.WebUnionidVo;
import ysq.xyg.web.service.WebUnionidService;

import javax.annotation.Resource;
import java.util.List;


/**
 * 业务管理-微信小程序关联表service实现
 *
 * @author shengQiang yu
 * @date 2019-5-18 20:48:41
 */
@Service("webUnionidService")
public class WebUnionidServiceImpl extends BaseServiceImpl<WebUnionid> implements WebUnionidService {

    @Resource
    private WebUnionidDao webUnionidDao;

    private WebUnionid webUnionid;

    @Autowired
    public void setBaseDao(WebUnionidDao webUnionidDao) {
        this.baseDao = webUnionidDao;
    }

    /**
     * 分页查询
     *
     * @param webUnionidVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-18 20:48:41
     */
    @Override
    public ReturnEntity page(WebUnionidVo webUnionidVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebUnionid> list = acceptList(webUnionidVo);
        PageInfo<WebUnionid> page=new PageInfo<WebUnionid>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webUnionidVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-18 20:48:41
     */
    @Override
    public ReturnEntity addOrUpdate(WebUnionidVo webUnionidVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webUnionidVo.getUnionid())){
            webUnionid=new WebUnionid(currentUser);
            isUpate=false;
        }else{
            webUnionid=selectByPrimaryKey(webUnionidVo.getUnionid());
            if(ObjectUtil.isNull(webUnionid)){
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
        }
        webUnionid.setXcxOpenid(webUnionidVo.getXcxOpenid());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webUnionid);
        }else{
            num=insert(webUnionid);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-18 20:48:41
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        int num=deleteByPrimaryKey(id);
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 获取集合
     *
     * @param webUnionidVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-5-18 20:48:41
     */
    @Override
    public List<WebUnionid> acceptList(WebUnionidVo webUnionidVo){
        Example example = new Example(WebUnionid.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webUnionidVo.getXcxOpenid())){
            criteria.andEqualTo("xcxOpenid",webUnionidVo.getXcxOpenid());
        }
        example.setOrderByClause(" update_time desc");
        return webUnionidDao.selectByExample(example);
    }
}
